﻿/*************************************************************
 * Software of blackcore
 *
 * CLRVersion:4.0.30319.296
 * MachineName:SPACE
 * Author:Andy Huang
 * Email:itblackhole@gmail.com
 * DateTime：5/17/2013 1:48:22 PM
 * Function：
 *************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SobFS.Common;

namespace SobFS.Web.UI
{
    public class BackendPage : System.Web.UI.Page
    {


        public BackendPage()
        {
            this.Load += new EventHandler(BackendPage_Load);
        }

        void BackendPage_Load(object sender, EventArgs e)
        {
            //TODO 如果未登录则要求重新登录
            if (GetSysUserInfo() == null)
            {
                string location = "http://" + Request.Url.Host + ":" + Request.Url.Port + "/backend/sign.aspx";           
                //Response.Write("<script>parent.location.href='sign.aspx'</script>");
                Response.Write("<script>parent.location.href='" + location + "'</script>");
                Response.End();
            }
        }

        #region ********************权限验证********************
        /// <summary>
        /// 获取登录用户信息
        /// </summary>
        public Model.SysUser GetSysUserInfo()
        {
            if (Sign())
            {
                Model.SysUser model = Session[SobFSConst.SESSION_BACKEND_USER] as Model.SysUser;
                if (model != null)
                {
                    return model;
                }
            }
            return null;
        }

        public bool Sign()
        {
            //当前session是否存在
            if (Session[SobFSConst.SESSION_BACKEND_USER] != null)
                return true;

            //TODO 从cookie中获取登录信息
            string loginInfo = CookieHelper.ReaderCookie(SobFSConst.COOKIE_BACKEND_USERNAME, SobFSConst.KEY_PASSWORD);
            string password = CookieHelper.ReaderCookie(SobFSConst.COOKIE_BACKEND_USERPASSWORD, SobFSConst.KEY_PASSWORD);
            return Sign(loginInfo, password);
        }

        /// <summary>
        /// 平台管理员登录
        /// </summary>
        /// <param name="loginInfo">可以使用username或email进行登录</param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool Sign(string loginInfo, string password)
        {
            //使用当前输入信息登录
            Model.SysUser model;
            BLL.SysUser bll = new BLL.SysUser();
            bool result = bll.Sign(loginInfo, password, out model);
            if (result)
            {
                Session[SobFSConst.SESSION_BACKEND_USER] = model;
            }
            return result;
        }
      
        public string GetAllPurStr(string GroupIDStr)
        {
            string StrPur = "";
            //string[] strArry = SplitGroupID(GroupIDStr.Trim());
            ////因为 8| 分解后成为2个,一个为8,一个为空
            //for (int i = 0; i < strArry.Length - 1; i++)
            //{
            //    int id = Convert.ToInt32(strArry.GetValue(i).ToString());
            //    if (id != 0)
            //    {
            //        StrPur += GetPurStrbyGroup(id);
            //    }
            //}
            return StrPur;
        }

        #region **********菜单生成**********
        public void BuildMenu()
        {
 
        }
       
        #endregion **********菜单生成**********

        #endregion ********************权限验证********************

        #region ********************JS提示信息********************

        /// <summary>
        /// 添加编辑删除提示
        /// </summary>
        /// <param name="msgtitle">提示文字</param>
        /// <param name="url">返回地址</param>
        /// <param name="msgcss">CSS样式</param>
        protected void JsMessageBox(string title, string url, JsMessageBoxCss css)
        {
            string msbox = "parent.jsprint(\"" + title + "\", \"" + url + "\", \"" + css.ToString() + "\")";
            ClientScript.RegisterClientScriptBlock(Page.GetType(), "JsPrint", msbox, true);
        }

        /// <summary>
        /// 带回传函数的添加编辑删除提示
        /// </summary>
        /// <param name="msgtitle">提示文字</param>
        /// <param name="url">返回地址</param>
        /// <param name="msgcss">CSS样式</param>
        /// <param name="callback">JS回调函数</param>
        protected void JsMessageBox(string title, string url, JsMessageBoxCss css, string callback)
        {
            string msbox = "parent.jsprint(\"" + title + "\", \"" + url + "\", \"" + css.ToString() + "\", " + callback + ")";
            ClientScript.RegisterClientScriptBlock(Page.GetType(), "JsPrint", msbox, true);
        }
        #endregion ********************JS提示信息********************
    }

    public enum JsMessageBoxCss
    {
        Success,
        Error
    }
}
